package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.godmch.parameter.getAgentPriceMarkupConfigBean;
import com.ymatou.iapi.godmch.parameter.setAgentConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.parameter.setAgentMarkupConfigBean;
import com.ymatou.iapi.godmch.service.getAgentPriceMarkupConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.markup_config_agent_price;
import com.ymttest.database.model.godmch.markup_config_agent_priceExample;
import com.ymttest.database.model.godmch.markup_config_percentage;
import com.ymttest.database.model.godmch.markup_config_percentageExample;
import com.ymttest.database.sqlwapper.godmch.markup_config_agent_priceWapper;
import com.ymttest.database.sqlwapper.godmch.markup_config_percentageWapper;
import com.ymttest.utils.TestDataManager;
/**
*获取商户代理价加价配置
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentPriceMarkupConfig{
	private static getAgentPriceMarkupConfigBean bean;
	private static getAgentPriceMarkupConfigCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商户代理价加价配置");
	}

	@Before
	public void caseUp() {
		bean=new getAgentPriceMarkupConfigBean();
		call=new getAgentPriceMarkupConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentPriceMarkupConfig_001() {
		//一个商户创建两条，中间会间隔1分钟，自动化中就不加2条的case   （参数异常, 抱歉，因商品数量比较多，有加价规则正在执行中，暂时无法修改。请稍后再试！）
		Logger.start(true,"获取商户代理价加价配置-代理价按条件加价规则-一条-返回成功");
		try {
			//第一条
			setAgentConditionMarkupConfigBean setBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setBean);
//			//第二条
//			setBean=GodmchManageCallService.initSetAgentConditionMarkupConfig(setBean.getMchId());
//			setBean.setCategoryList(Lists.newArrayList(TestDataManager.getRandomIntNum(5)));
//			GodmchManageCallService.setAgentConditionMarkupConfigCall(setBean);
			bean.setMchId(setBean.getMchId());
			call.setData(bean);
			call.callService();	
			
			markup_config_agent_priceWapper markupConfig=new markup_config_agent_priceWapper();
			markup_config_agent_priceExample example=new markup_config_agent_priceExample();
			example.createCriteria().andMch_idEqualTo(setBean.getMchId());
			List<markup_config_agent_price> configs=markupConfig.selectByExample(example);
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(configs.get(0).getMarkup_id(),  call.getConditionList().getJSONObject(0).getInt("markupId"), "验证第一条Markup_id");
			//Logger.verifyEquals(configs.get(1).getMarkup_id(), call.getConditionList().getJSONObject(0).getInt("markupId"), "验证第二条Markup_id");
			

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentPriceMarkupConfig_002() {
		Logger.start(true,"获取商户代理价加价配置-全局-一条-返回成功");
		try {
			//第一条
			setAgentMarkupConfigBean setBean=GodmchManageCallService.initSetAgentMarkupConfigBean();
			GodmchManageCallService.initSetAgentMarkupConfigCall(setBean);
			//第二条
			setBean=GodmchManageCallService.initSetAgentMarkupConfigBean(setBean.getMchId());
			setBean.getPercentageList().get(0).setPercentage(new BigDecimal(20));
			GodmchManageCallService.initSetAgentMarkupConfigCall(setBean);
			bean.setMchId(setBean.getMchId());
			call.setData(bean);
			call.callService();	
			
			
			markup_config_agent_priceWapper markupConfig=new markup_config_agent_priceWapper();
			markup_config_agent_priceExample example=new markup_config_agent_priceExample();
			example.createCriteria().andMch_idEqualTo(setBean.getMchId());
			List<markup_config_agent_price> configs=markupConfig.selectByExample(example);
			//markup_config_percentage
			markup_config_percentageWapper percentWapper=new markup_config_percentageWapper();
			markup_config_percentageExample percentageExample=new markup_config_percentageExample();
			percentageExample.createCriteria().andMarkup_idEqualTo(configs.get(0).getMarkup_id());
			List<markup_config_percentage> percentageLst=percentWapper.selectByExample(percentageExample);
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(percentageLst.get(0).getAgent_type_id(),  call.getGlobalConfig().getJSONObject(0).getInt("agentTypeId"), "验证第一条agentTypeId");
			Logger.verifyEquals(percentageLst.get(0).getPercentage().stripTrailingZeros().toPlainString(),  new BigDecimal(call.getGlobalConfig().getJSONObject(0).getInt("percentage")).stripTrailingZeros().toPlainString(), "验证第一条agentTypeId");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_getAgentPriceMarkupConfig_003() {
		//一个商户创建两条，中间会间隔1分钟，自动化中就不加2条的case   （参数异常, 抱歉，因商品数量比较多，有加价规则正在执行中，暂时无法修改。请稍后再试！）
		Logger.start(true,"获取商户代理价加价配置-全局+代理条件-2条-返回成功");
		try {
			//第一条
			setAgentConditionMarkupConfigBean setBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setBean);
			//第二条
			setBean=GodmchManageCallService.initSetAgentConditionMarkupConfig(setBean.getMchId());
			setBean.setCategoryList(Lists.newArrayList(TestDataManager.getRandomIntNum(5)));
			setBean.getPercentageList().get(0).setPercentage(new BigDecimal(20));
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setBean);
			
//			//第3条
//			setAgentMarkupConfigBean setConfigBean=GodmchManageCallService.initSetAgentMarkupConfigBean(setBean.getMchId());
//			GodmchManageCallService.initSetAgentMarkupConfigCall(setConfigBean);

			bean.setMchId(setBean.getMchId());
			call.setData(bean);
			call.callService();	
			
			markup_config_agent_priceWapper markupConfig=new markup_config_agent_priceWapper();
			markup_config_agent_priceExample example=new markup_config_agent_priceExample();
			example.createCriteria().andMch_idEqualTo(setBean.getMchId());
			List<markup_config_agent_price> configs=markupConfig.selectByExample(example);
			//markup_config_percentage
			markup_config_percentageWapper percentWapper=new markup_config_percentageWapper();
			markup_config_percentageExample percentageExample=new markup_config_percentageExample();
			percentageExample.createCriteria().andMarkup_idEqualTo(configs.get(0).getMarkup_id());
			List<markup_config_percentage> percentageLst=percentWapper.selectByExample(percentageExample);
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(percentageLst.get(0).getAgent_type_id(),  call.getGlobalConfig().getJSONObject(0).getInt("agentTypeId"), "验证第一条agentTypeId");
			Logger.verifyEquals(percentageLst.get(0).getPercentage().stripTrailingZeros().toPlainString(),   new BigDecimal(call.getGlobalConfig().getJSONObject(0).getInt("percentage")).stripTrailingZeros().toPlainString(), "验证第一条agentTypeId");
			
			Logger.verifyEquals(configs.get(0).getMarkup_id(),  call.getConditionList().getJSONObject(0).getInt("markupId"), "验证第一条Markup_id");
		//	Logger.verifyEquals(configs.get(1).getMarkup_id(), call.getConditionList().getJSONObject(0).getInt("markupId"), "验证第一条Markup_id");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}